//当角色信息发生改变时,改变flag变量
var roleInfoflag = false;

var containArray = "";

var list;


window.onload = function () {
    //放到项目中放开
    var a = location.href;
    var cc = parseInt(a.indexOf("roleId="))+7;
    var zz = a.length;
    var roleId = a.substring(cc, zz)
    console.log(roleId)


    //var roleId = 16; //研发默认roleId为13,并到项目中注释
    // 获取本地存储对象
    var localStorage = window.localStorage;
    var confirmMID = localStorage.getItem("key");

    //该角色id的信息渲染
    $.post("http://localhost:8081/study/permission/roleDetail", {
        roleId: roleId
    }, function(data) {
        console.log(data);
        if(data.success) {
            var role = data.data.role;
            list = data.data.list;
            //将list数据循环转成string类型,利用indexof 来实现java map中contain一样的功能.
            console.log(list)
            //将role信息添加到对应的位置
            $("#roleName").attr("value", role.roleName).attr("onchange", "changeFlag()");
            $("#remark").attr("value", role.remark).attr("onchange", "changeFlag()");

            //默认选中实现
            console.log(role)

            $.each(list, function(index, data) {
                containArray += data + ","
            })
            console.log(containArray)
            //找到访问路径tree下面所有input标签
            var treeHtml = $("#contentTree").find("input");
            console.log(treeHtml)
            $.each(treeHtml, function(index, data) {
                var value = $(data).val();
                console.log("pathId::" + value);
                if(containArray.indexOf(value) >= 0) {
                    $(data).attr("checked", "checked");
                } else {
                    $(data).removeAttr("checked");
                }
            });
        }
    })

    //提交
    $("#roleDetailSubmit").click(function() {
        console.log(roleInfoflag)
        //如果做过修改则重新生成该角色的相关信息
        var checkBoxArray = $(".choose");
        var permissionList = "";
        $.each(checkBoxArray, function(index, data) {
            var permission = $(data).val();
            console.log(permission);
            if(containArray.indexOf(permission) <0){
                roleInfoflag = true;
            }
            var ss = $(data).attr("checked");
            if(ss == "checked") {
                permissionList += permission + ","
            }
        });
        console.log(permissionList);
        //判断路径标签是否有变化
        $.each(list, function(index, data) {
            console.log(data)
            var zz = data+"";
            var juge = permissionList.indexOf(zz);
            if(juge < 0) {
                roleInfoflag = true;
            }
        })
        console.log(roleInfoflag)
        //如果路径发生变化,则重新调用接口
        if(roleInfoflag) {
            //重新生成role请求
            var roleName = $("#roleName").val();
            var remark = $("#remark").val();
            console.log(checkBoxArray.length)

            $.post("http://localhost:8081/study/permission/updateRoleDetail", {
                confirmMID: confirmMID,
                id: roleId,
                roleName: roleName,
                remark: remark,
                permissionList: permissionList
            }, function(data) {
                console.log(data);
                if(data.success) {
                    alert(data.message)
                    window.location.href = "html/findAllRole.html"
                } else {
                    alert(data.message)
                }
            })
        } else {
            //为修改则返回查询角色页面
            window.location.href = "html/findAllRole.html"
        }
    })

}

function changeFlag() {
    roleInfoflag = true;
}